// pages/movie/movie.js
const WXAPI = require('apifm-wxapi')
WXAPI.init('sqdaxiang')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannersComing:{},
    banners: {},
    condition:1,
    movieJson: [
      {
        "id":0,
        "totalPrice":0,
        "price":40,
        "count":0,
        "photo":"https://img1.utuku.imgcdc.com/650x0/ent/20250115/703dbb98-3cfd-40ff-b9cd-6338d60b0020.jpg",
        "name": "哪吒之魔童闹海",
        "grade": 9.7,
        "appraise": 229.8,
        "director": "饺子",
        "protagonist": "吕艳婷"
      },
      {
        "id":1,
        "totalPrice":0,
         "price":40,
          "count":0,
          "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa334fcea46e315d53de210ad383961d01e4.jpg?imageView2/1/w/464/h/644",
          "name": "唐探1900",
          "grade": 9.3,
          "appraise": 46.1,
          "director": "陈思成",
          "protagonist": "王宝强"
      },
      {
        "id":2,
        "totalPrice":0,
        "price":40,
        "count":0,
        "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa334520516e35c293e79c1b2c4ffbc62f79.jpg?imageView2/1/w/464/h/644",
        "name": "熊出没·重启未来",
        "grade": 9.4,
        "appraise": 12.2,
        "director": "林永长",
        "protagonist": "熊大、熊二"
      },
      {
        "id":3,
        "totalPrice":0,
        "price":40,
        "count":0,
        "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa334fc6e36e377e115d52c4fa4f28363cec.jpg?imageView2/1/w/464/h/644",
        "name": "您的声音",
        "grade": 9.1,
        "appraise": 835,
        "director": "毕龙君、周丹丹",
        "protagonist": "樱井孝宏、福山润"
      }
  ],
  movieComingArray:[
    {
      "id":0,
      "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa3344e15d3cb8dd477e120c944579005b1e.jpg?imageView2/1/w/464/h/644",
      "name": "水饺皇后",
      "appraise":184955,
      "director": "刘伟强",
      "protagonist": "马丽",
      "booking":"预约",
      "release":"4月30日",
      "status": "bookable"
    },
    {
      "id":1,
      "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa3343d8d315d15d5f87b55aa435ad11de73.jpg?imageView2/1/w/464/h/644",
      "name": "雷霆特攻队*",
      "appraise":74522,
      "director": "杰克·施莱尔",
      "protagonist": "弗洛伦斯·皮尤",
      "booking":"预约",
      "release":"4月30日",
      "status": "bookable"
    },
    {
      "id":2,
      "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa3343df874a1d8a1ce63ffc016f34e06865.jpg?imageView2/1/w/464/h/644",
      "name": "黑暗迷踪",
      "director": "柯伊善",
      "appraise":4181,
      "protagonist": "朱宏",
      "booking":"预约",
      "release":"4月30日",
      "status": "bookable" 
    },
    {
      "id":3,
      "name": "猎金·游戏",
      "photo":"https://p0.pipi.cn/mediaplus/friday_image_fe/0fa3343df87fcfa219704862d9f89854c3059.jpg?imageView2/1/w/464/h/644",
      "director": "邱礼涛",
      "appraise":111888,
      "protagonist": "刘德华",
      "booking":"预约",
      "release":"5月1日",
      "status": "bookable" 
    }
  ]
  },
  add(event){
    console.log(event);
    var mOATmp = this.data.movieJson
    var movieID = event.currentTarget.dataset.id
    if(mOATmp[movieID].count < 10){
      mOATmp[movieID].count += 1
      mOATmp[movieID].totalPrice = mOATmp[movieID].count * mOATmp[movieID].price
    }
    this.setData({
      movieJson: mOATmp
    })
  },
  sub(event) {
    console.log(event);
    var mOATmp = this.data.movieJson
    var movieID = event.currentTarget.dataset.id
    if (mOATmp[movieID].count > 0) {
      mOATmp[movieID].count -= 1
      mOATmp[movieID].totalPrice = mOATmp[movieID].count * mOATmp[movieID].price
    }
    this.setData({
      movieJson: mOATmp
    })
  },
  buyTicket(e){
    var name = e.currentTarget.dataset.name;
    wx.setStorageSync('movieName',name)
    wx.navigateTo({
      url: '/pages/seatChoose/seatChoose',
    })
  },
  switchTag(event){
    var id = event.currentTarget.dataset.id;
    console.log(id);
    if(id == 1){
      this.setData({
        condition: 2
      })
    }else{
      this.setData({
        condition: 1
      })
    }
  },
  bookTicket(event) {
    const movieID = event.currentTarget.dataset.id;
    const currentArray = [...this.data.movieComingArray];
    const targetMovie = currentArray.find(item => item.id === movieID);
  
    if (targetMovie.status === "bookable") {
      targetMovie.appraise = targetMovie.appraise + 1;
      targetMovie.status = "booked";
      targetMovie.booking = "已预约";
    } else if (targetMovie.status === "booked") {
      wx.showModal({
        title: '确认取消',
        content: '确定要取消该电影的预约吗？',
        success: (res) => {
          if (res.confirm) {
            if (targetMovie.appraise > 0) {
              targetMovie.appraise = targetMovie.appraise - 1;
            }
            targetMovie.status = "bookable";
            targetMovie.booking = "预约";
            
            this.setData({
              movieComingArray: currentArray 
            });
            
            wx.showToast({ title: '已取消预约', icon: 'none' });
          }
        }
      });
      return; 
    }
  
    this.setData({
      movieComingArray: currentArray
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    WXAPI.banners().then(res => {
      
      if(res.code == 0) {
        const photoBanners = res.data
        .filter(item => item.type === 'carouselNow')
        .map((item, index) => ({
          ...item,
          id: `banner_${index}` // 添加唯一id
        }));
        const ComingBanners = res.data
        .filter(item => item.type === 'carouselComing')
        .map((item, index) => ({
          ...item,
          id: `banner_${index}` // 添加唯一id
        }));
        this.setData({
          banners: photoBanners,
          bannersComing:ComingBanners
        })
      }
      console.log(this.data.banners);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})